/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

@import '../commons/index.css';

:root {
  /* Hardcoded for wrapping study.
  Will be a DNA token in https://jira.corp.adobe.com/browse/SDS-4466 */
  --spectrum-switch-label-margin-top: var(--spectrum-global-dimension-size-65);

  /* Fix for inconsistent line-height between browsers that would push the label 1px below the intended baseline */
  /* more info: https://stackoverflow.com/questions/47700568/css-fonts-render-differently-in-firefox-and-chrome */
  --spectrum-switch-label-line-height: 1.49;
}

.spectrum-ToggleSwitch {
  display: inline-flex;
  align-items: flex-start;

  position: relative;

  min-block-size: var(--spectrum-switch-height);
  max-inline-size: 100%;

  margin-inline-end: calc(var(--spectrum-switch-cursor-hit-x) * 2);

  vertical-align: top;

  isolation: isolate;
}

.spectrum-ToggleSwitch-input {
  /* Remove the margin for input in Firefox and Safari. */
  margin: 0;

  /* Add the correct box sizing for [type="checkbox"] in IE 10-. */
  box-sizing: border-box;

  /* Remove the padding for [type="checkbox"] in IE 10-. */
  padding: 0;

  position: absolute;
  inline-size: 100%;
  block-size: 100%;
  inset-block-start: 0;
  inset-inline-start: 0;
  opacity: .0001;
  z-index: 1;

  /* prevent double click event due to browser waiting to fire double tap */
  touch-action: manipulation;
  cursor: default;

  &:checked + .spectrum-ToggleSwitch-switch {
    [dir='ltr'] &::before {
      transform: translateX(calc(var(--spectrum-switch-track-width) - 100%));
    }
    [dir='rtl'] &::before {
      transform: translateX(calc(100% - var(--spectrum-switch-track-width)));
    }
  }

  &:disabled,
  &[disabled] {
    cursor: default;
  }

  &:focus-visible {
    + .spectrum-ToggleSwitch-switch {
      &:after {
        margin: calc(var(--spectrum-alias-focus-ring-gap) * -1);
      }
    }
  }
}

.spectrum-ToggleSwitch-label {
  text-align: start;
  margin: 0 var(--spectrum-switch-text-gap);
  margin-block-start: var(--spectrum-switch-label-margin-top);
  font-size: var(--spectrum-switch-text-size);
  line-height: var(--spectrum-switch-label-line-height);
  transition: color var(--spectrum-global-animation-duration-200) ease-in-out;
}

.spectrum-ToggleSwitch-switch {
  display: inline-block;
  box-sizing: border-box;
  /* positions the pseudo elements relative to this one */
  position: relative;

  inline-size: var(--spectrum-switch-track-width);

  /* Fix vertical alignment when not wrapping since we're flex-start */
  margin: calc((var(--spectrum-switch-height) - var(--spectrum-switch-track-height)) / 2) 0;

  flex-grow: 0;
  flex-shrink: 0;

  vertical-align: middle;

  /* Forms stacking context for Firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1510479
  Fix for scaling all elemetns with transform property equally on browser zoom */
  will-change: transform;

  transition: background var(--spectrum-global-animation-duration-100) ease-in-out, border var(--spectrum-global-animation-duration-100) ease-in-out;

  block-size: var(--spectrum-switch-track-height);

  inset-inline-start: 0;
  inset-inline-end: 0;

  border-radius: calc(var(--spectrum-switch-track-height) / 2);

  &:before {
    display: block;
    position: absolute;
    content: "";
    box-sizing: border-box;
  }

  /* :before is used for the handle of the switch */
  &:before {
    transition: background var(--spectrum-global-animation-duration-100) ease-in-out, border var(--spectrum-global-animation-duration-100) ease-in-out, transform var(--spectrum-global-animation-duration-100) ease-in-out, box-shadow var(--spectrum-global-animation-duration-100) ease-in-out;

    inline-size: var(--spectrum-switch-handle-size);
    block-size: var(--spectrum-switch-handle-size);

    inset-block-start: 0;
    inset-inline-start: 0;

    border-width: var(--spectrum-switch-handle-border-size);
    border-radius: calc(var(--spectrum-switch-handle-size) / 2);
    border-style: solid;
  }

  /* :after is used for the focus halo */
  &:after {
    border-radius: calc(calc(var(--spectrum-switch-track-height) / 2) + var(--spectrum-alias-focus-ring-gap) * 2);
    content: '';
    display: block;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    margin: 0;
    transform: translateX(0); /* Forces FF to render on the pixel grid */

    transition: box-shadow var(--spectrum-global-animation-duration-100) ease-out,
                margin var(--spectrum-global-animation-duration-100) ease-out;
  }
}
